package java3.io;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class RobotChess {

    public static final int alto = 55;
    public static final int ancho = 55;
    public static final int posX = 200;
    public static final int posY = 175;
    public static final int relojX = 1338;
    public static final int relojY = 700;

    public static void realizarJugada(String paramString, boolean paramBoolean1, boolean paramBoolean2) {
        moverPieza(convertirJugada(paramString, paramBoolean1), paramBoolean2 ? obtenerDelay() : 0);
    }

    private static void moverPieza(int[] jugada, int delay) {
        try {
            Robot localRobot = new Robot();

            localRobot.delay(delay);
            localRobot.mouseMove(jugada[0], jugada[1]);
            localRobot.mousePress(16);
            localRobot.mouseMove(jugada[2], jugada[3]);
            localRobot.mouseRelease(16);

            localRobot.mouseMove(relojX, relojY);
        } catch (AWTException localAWTException) {
            System.out.println("ERROR RobotChess - moverPieza: " + localAWTException);
            localAWTException.printStackTrace();
        }
    }

    public static void realizarJugadaLoser(int[] jugada, boolean isWhite, boolean existDelay) {
        moverPiezaLoser(convertirJugadaLoser(jugada, isWhite), existDelay ? obtenerDelayLoser() : 0);
    }

    private static void moverPiezaLoser(int[] jugada, int delay) {
        try {
            Robot localRobot = new Robot();

            if (jugada[4] != 0) {
                int key = obtenerPromocion(jugada[4]);
                localRobot.keyPress(key);
                localRobot.keyRelease(key);
            }

            localRobot.delay(delay);
            localRobot.mouseMove(jugada[0], jugada[1]);
            localRobot.mousePress(16);
            localRobot.mouseMove(jugada[2], jugada[3]);
            localRobot.mouseRelease(16);

            localRobot.mouseMove(relojX, relojY);
        } catch (AWTException localAWTException) {
            System.out.println("ERROR RobotChess - moverPieza: " + localAWTException);
            localAWTException.printStackTrace();
        }
    }

    private static int[] convertirJugada(String paramString, boolean isBoardReverse) {
        int[] arrayOfInt = new int[4];
        char[] arrayOfChar = paramString.toCharArray();

        for (int i = 0; i < arrayOfChar.length; i++) {
            char c = arrayOfChar[i];

            if ((i == 0) || (i == 2)) {
                switch (c) {
                    case 'a':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 0 : 7) * ancho);
                        break;
                    case 'b':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 1 : 6) * ancho);
                        break;
                    case 'c':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 2 : 5) * ancho);
                        break;
                    case 'd':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 3 : 4) * ancho);
                        break;
                    case 'e':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 4 : 3) * ancho);
                        break;
                    case 'f':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 5 : 2) * ancho);
                        break;
                    case 'g':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 6 : 1) * ancho);
                        break;
                    case 'h':
                        arrayOfInt[i] = (posX + (isBoardReverse ? 7 : 0) * ancho);
                }
            } else if ((i == 1) || (i == 3)) {
                switch (Integer.parseInt(String.valueOf(c))) {
                    case 1:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 7 : 0) * alto);
                        break;
                    case 2:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 6 : 1) * alto);
                        break;
                    case 3:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 5 : 2) * alto);
                        break;
                    case 4:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 4 : 3) * alto);
                        break;
                    case 5:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 3 : 4) * alto);
                        break;
                    case 6:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 2 : 5) * alto);
                        break;
                    case 7:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 1 : 6) * alto);
                        break;
                    case 8:
                        arrayOfInt[i] = (posY + (isBoardReverse ? 0 : 7) * alto);
                }
            }

        }

        return arrayOfInt;
    }

    private static int[] convertirJugadaLoser(int[] jugada, boolean isWhite) {
        int[] pixeles = new int[5];

        for (int i = 0; i < 2; i++) {
            int val = jugada[i + 1];

            pixeles[i * 2] = (posX + (isWhite ? val % 8 : 7 - val % 8) * ancho);
            pixeles[i * 2 + 1] = (posY + (isWhite ? 7 - (val / 8) : val / 8) * alto);
        }

        if (jugada.length > 3) {
            pixeles[4] = jugada[3];
        }

        return pixeles;
    }

    private static int GetRnd(int paramInt) {
        return (int) (Math.random() * 32000.0D) % paramInt;
    }

    private static int obtenerDelay() {
        int i = 0;

        int j = GetRnd(10);

        switch (j) {
            case 0:
                i = 1000;
                break;
            case 1:
                i = 500;
                break;
            case 2:
                i = 700;
                break;
            case 3:
                i = 500;
                break;
            case 4:
                i = 1500;
                break;
            case 5:
                i = 500;
                break;
            case 6:
                i = 1000;
                break;
            case 7:
                i = 500;
                break;
            case 8:
                i = 1000;
                break;
            case 9:
                i = 500;
                break;
            default:
                i = -1;
                System.out.println("**you play! x default!!");
        }
        return i;
    }

    private static int obtenerDelayLoser() {
        int i = 0;

        int j = GetRnd(10);

        switch (j) {
            case 0:
                i = 2000;
                break;
            case 1:
                i = 500;
                break;
            case 2:
                i = 1000;
                break;
            case 3:
                i = 500;
                break;
            case 4:
                i = 500;
                break;
            case 5:
                i = 1000;
                break;
            case 6:
                i = 500;
                break;
            case 7:
                i = 1000;
                break;
            case 8:
                i = 500;
                break;
            case 9:
                i = 1000;
                break;
            default:
                i = 1000;
        }
        return i;
    }

    private static int obtenerPromocion(int i) {
        switch (i) {
            case 2:
                return KeyEvent.VK_N;
            case 3:
                return KeyEvent.VK_B;
            case 4:
                return KeyEvent.VK_R;
            case 5:
                return KeyEvent.VK_Q;
        }
        return KeyEvent.VK_X;
    }
}